<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="9" :xs="24">
        <el-card>
          <div
            slot="header"
            style="display: flex; justify-content: space-between"
          >
            <span> 标签组 </span>
          </div>
          <BiTalentLabelGroup @tableEdit="tableEdit" />
          <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini"
                >新增</el-button
              >
            </el-col>
          </el-row>
          <el-table :data="purchaseDocumentCodeList" @cell-click="tableEdit">
            <el-table-column label="标签组" align="center" prop="label" />
          </el-table> -->
        </el-card>
      </el-col>

      <!--用户数据-->
      <el-col :span="15" :xs="24">
        <el-card>
          <div
            slot="header"
            style="display: flex; justify-content: space-between"
          >
            <span
              >标签组名字: {{ row ? row.labelGroupName + " " : "所有" }}
            </span>
          </div>
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="false"
            label-width="68px"
            @submit.native.prevent
          >
            <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model.trim="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="标签名称" prop="tagName">
              <el-input
                v-model.trim="queryParams.tagName"
                placeholder="请输入标签名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- <el-form-item label="标签组" prop="tagGroupId">
              <el-input
                v-model.trim="queryParams.tagGroupId"
                placeholder="请输入标签组"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item> -->
            <!-- <el-form-item label="联系人数" prop="contactCount">
        <el-input
          v-model.trim="queryParams.contactCount"
          placeholder="请输入联系人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联达人数" prop="associatedReachCount">
        <el-input
          v-model.trim="queryParams.associatedReachCount"
          placeholder="请输入关联达人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联地址数" prop="relatedAddressCount">
        <el-input
          v-model.trim="queryParams.relatedAddressCount"
          placeholder="请输入关联地址数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button size="mini" type="primary" @click="handleUpdate(row)"
                >新增标签</el-button
              >
            </el-col>
            <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['talent:BiTalentLabel:edit']"
              >修改</el-button
            >
          </el-col> -->
            <!-- <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['talent:BiTalentLabel:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['talent:BiTalentLabel:export']"
              >导出</el-button
            >
          </el-col> -->
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </el-row>

          <el-table
            v-loading="loading"
            :data="BiTalentLabelList"
            @selection-change="handleSelectionChange"
            :height="550"
          >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <!-- <el-table-column label="id" align="center" prop="id" /> -->
            <!-- <el-table-column label="逻辑删除" align="center" prop="isDelete" /> -->
            <!-- <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="标签名称" align="center" prop="tagName" />
          <el-table-column label="标签组" align="center" prop="tagGroupId" />
           -->
            <el-table-column label="标签名称" align="center" prop="labelName" />
            <el-table-column
              label="联系人数"
              align="center"
              prop="associatedReachCount"
            />
            <el-table-column
              label="关联达人数"
              align="center"
              prop="contactCount"
            >
              <template slot-scope="scope">
                <div
                  style="
                    color: rgb(37, 137, 255);
                    cursor: pointer;
                    text-decoration: underline;
                  "
                  @click="handlelabelName(scope.row)"
                >
                  {{ scope.row.contactCount }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="关联地址数"
              align="center"
              prop="relatedAddressCount"
            />
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <!-- <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['talent:BiTalentLabel:edit']"
                  >修改</el-button
                > -->
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['talent:BiTalentLabel:remove']"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改达人标签组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签组名" prop="labelGroupName">
          <el-input
            :disabled="true"
            v-model.trim="form.labelGroupName"
            placeholder="请输入标签组名"
          />
        </el-form-item>
        <el-form-item label="标签名" prop="labelName">
          <!-- <el-input
            v-model.trim="form.labelNameList"
            placeholder="请输入标签名"
          /> -->
          <el-select
            v-model="form.labelNameList"
            placeholder=""
            filterable
            multiple
          >
            <el-option
              v-for="(dict, i) in TalentLabelList"
              :key="i"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="openSample"
      width="1500px"
      append-to-body
      title="关联达人"
      v-if="openSample"
    >
      <component :is="'sample'" :row="row"></component>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiTalentLabelGroup,
  getBiTalentLabelGroup,
  delBiTalentLabelGroup,
  addBiTalentLabelGroup,
  updateBiTalentLabelGroup,
  labelGroupAddLabel,
  deleteLabelGroup,
  updateLabelGroup,
  deleteLabelGroupLabel,
  selectGroupLabelList,
} from "@/api/talent/BiTalentLabelGroup";
import {
  selectLabelGroupList,
  listBiTalentLabel,
  getBiTalentLabel,
  delBiTalentLabel,
  addBiTalentLabel,
  updateBiTalentLabel,
} from "@/api/talent/BiTalentLabel";
// src/views/talent/BiTalentLabelGroup/index.vue
import BiTalentLabelGroup from "@/views/talent/BiTalentLabelGroup/index.vue";
import {
  releaseTalent,
  distributionTalent,
  setLabel,
  talentBlock,
  selectTalentLabel,
} from "@/api/talent/Detail";
import sample from "./sample"; // 新建页面
export default {
  name: "BiTalentLabel",
  components: {
    BiTalentLabelGroup,
    sample,
  },
  data() {
    return {
      openSample: false,
      TalentLabelList: [],
      row: null,
      purchaseDocumentCodeList: [{ label: "标签1" }],
      inputValue: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 达人-标签表格数据
      BiTalentLabelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isDelete: null,
        tagName: null,
        tagGroupId: null,
        contactCount: null,
        associatedReachCount: null,
        relatedAddressCount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    selectLabelGroupList().then((response) => {
      this.purchaseDocumentCodeList = [];
      response.rows.forEach((item) => {
        this.purchaseDocumentCodeList.push({ label: item });
      });
    });
    this.getList();
  },
  methods: {
    handlelabelName(row) {
      this.row = row;
      this.openSample = true;
    },
    tableEdit(row, column, cell, event) {
      // this.checkpurchaseDocumentCode = row.purchaseDocumentCode;
      this.row = row;
      this.queryParams.labelGroupName = row.labelGroupName;
      this.getList();
    },
    /** 查询达人-标签列表 */
    getList() {
      this.loading = true;
      listBiTalentLabel(this.queryParams).then((response) => {
        this.BiTalentLabelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        labelNameList: [],
        labelGroupName: null,
        labelName: null,
        // createBy: null,
        // createTime: null,
        // updateBy: null,
        // updateTime: null,
        // isDelete: null,
        // remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加达人-标签";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //   const id = row.id || this.ids;
      //   getBiTalentLabelGroup(id).then((response) => {
      //     this.form = response.data;
      if (!this.row) {
        this.$modal.msgError("请选择左边标签组一条数据");
        return;
      }
      if (row) {
        this.form.labelGroupName = row.labelGroupName;
      }
      selectTalentLabel().then((response) => {
        this.TalentLabelList = response;
      });
      selectGroupLabelList({ labelGroupName: row.labelGroupName }).then(
        (response) => {
          // this.form.labelNameList = [];
          this.form.labelNameList = response;
          this.open = true;
        }
      );

      // this.title = "修改达人标签组";
      //   });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.labelNameList = this.form.labelNameList.join(",");
      labelGroupAddLabel(this.form).then((response) => {
        this.$modal.msgSuccess("新增成功");

        this.getList();
        this.open = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (!this.row) {
        this.$modal.msgError("请选择左边标签组一条数据");
        return;
      }
      const ids = {
        labelName: row.labelName,
        labelGroupName: this.row.labelGroupName,
      };
      this.$modal
        .confirm("是否确认删除达人-标签的数据项？")
        .then(function () {
          return deleteLabelGroupLabel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "talent/BiTalentLabel/export",
        {
          ...this.queryParams,
        },
        `BiTalentLabel_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
